// 322.零钱兑换
package LeetcodeHot100;

public class Solution322 {
    public int coinChange(int[] coins, int amount) {
        // dp[i] 表示纸币的最小数量
        int[] dp = new int[amount + 1];
        // 初始化
        for (int i = 1; i < amount + 1; i++) {
            dp[i] = -1;
            for (int j = 0; j < coins.length; j++) {
                if (coins[j] == i) {
                    dp[i] = 1;
                    continue;
                }
            }
        }
        // for (int i = 1; i < amount + 1; i++) {
        // System.out.println(dp[i]);
        // }

        // System.out.println("@@@@@");

        for (int i = 1; i < amount + 1; i++) {
            if (dp[i] != -1)
                continue;
            int min = Integer.MAX_VALUE;
            for (int j = 0; j < coins.length; j++) {
                if (coins[j] < i && dp[i - coins[j]] != -1) {
                    min = Math.min(min, dp[i - coins[j]] + 1);
                }
            }
            dp[i] = min == Integer.MAX_VALUE ? -1 : min;
        }
        // for (int i = 1; i < amount + 1; i++) {
        // System.out.println(dp[i]);
        // }
        return dp[amount];
    }
}
